import logging
from datetime import timedelta
from rest_framework import viewsets, permissions, status
from rest_framework.response import Response
from rest_framework.authentication import TokenAuthentication
from django.utils import timezone
from django.contrib.auth.models import User
from appinput.models import App
from serverinput.models import Server
from deploy.models import DeployPool
from envx.models import Env
from .serializers import ServerSerializer, DeployPoolSerializer, AppSerializer, UserSerializer


mylog = logging.getLogger('automan_default')


class ServerViewSet(viewsets.ModelViewSet):
    """This viewset automatically provides `list` and `detail` actions."""
    queryset = Server.objects.all()
    serializer_class = ServerSerializer
    authentication_classes = (TokenAuthentication,)
    permission_classes = (permissions.IsAuthenticatedOrReadOnly, )

    # 重写create和update方法，实现外键方面的关联
    def create(self, request, *args, **kwargs):
        try:
            ta = TokenAuthentication()
            user_name, token = ta.authenticate(request)
            print(user_name, token)
        except Exception as e:
            print(e)
            result = {'return': 'fail', 'message': 'auth fail.'}
            return Response(data=result, status=403)

        if user_name != request.user:
            result = {'return': 'fail', 'message': 'others token.'}
            return Response(data=result, status=403)

        validated_data = dict()
        validated_data['name'] = request.data['name']
        validated_data['ip_address'] = request.data['ip_address']
        validated_data['port'] = request.data['port']
        validated_data['salt_name'] = request.data['salt_name']
        validated_data['app_name'] = App.objects.get(name=request.data['app_name'])
        validated_data['env_name'] = Env.objects.get(name=request.data['env_name'])
        validated_data['app_user'] = request.data['app_user']
        validated_data['op_user'] = request.user

        try:
            Server.objects.create(**validated_data)
            mylog.debug("create server is {}.".format(validated_data))
            response_data = {'result': 'success', 'message': u'新服务器插入数据库成功！'}
            return Response(data=response_data, status=status.HTTP_201_CREATED)
        except Exception as e:
            print(e)
            response_data = {'result': 'failed', 'message': u'不能正确插入数据库！'}
            return  Response(data=response_data, status=status.HTTP_400_BAD_REQUEST)

    def update(self, request, *args, **kwargs):
        try:
            ta = TokenAuthentication()
            user_name, token = ta.authenticate(request)
        except Exception as e:
            print(e)
            result = {'return': 'fail', 'message': 'auth fail.'}
            return Response(data=result, status=403)

        if user_name != request.user:
            result = {'return': 'fail', 'message': 'others token.'}
            return Response(data=result, status=403)

        validated_data = dict()
        validated_data['name'] = request.data['name']
        validated_data['ip_address'] = request.data['ip_address']
        validated_data['port'] = request.data['port']
        validated_data['salt_name'] = request.data['salt_name']
        validated_data['app_name'] = App.objects.get(name=request.data['app_name'])
        validated_data['env_name'] = Env.objects.get(name=request.data['env_name'])
        validated_data['app_user'] = request.data['app_user']
        validated_data['op_user'] = request.user

        pk_id = kwargs["pk"]
        try:
            server_item = Server.objects.filter(pk=pk_id)
            server_item.update(**validated_data)
            mylog.debug("update server {} is {}.".format(pk_id, validated_data))
            response_data = {'result': 'success', 'name': pk_id, 'create_user': request.user.username, 'message': u'更新服务器成功！'}
            return Response(data=response_data, status=status.HTTP_201_CREATED)
        except Exception as e:
            print(e)
            response_data = {'result': 'failed', 'message': u'更新服务器失败！'}
            return Response(data=response_data, status=status.HTTP_400_BAD_REQUEST)


class DeployPoolViewSet(viewsets.ModelViewSet):
    """
    This viewset automatically provides `list`, `create`, `retrieve`, `update`, and `destroy` actions.
    Additionally we also provide an extra `highlight` action.
    """
    serializer_class = DeployPoolSerializer

    def get_queryset(self):
        filter_dict = dict()
        current_date = timezone.now()
        filter_dict['change_date__gt'] = current_date - timedelta(days=365)
        return DeployPool.objects.filter(**filter_dict)


class AppViewSet(viewsets.ModelViewSet):
    """This viewset automatically provides `list` and `detail` actions."""
    queryset = App.objects.all()
    serializer_class = AppSerializer
    authentication_classes = (TokenAuthentication, )
    permission_classes = (permissions.AllowAny, )

    # 重写create和update方法，用于实现外键方面的关联
    def create(self, request, *args, **kwargs):
        try:
            ta = TokenAuthentication()
            user_name, token = ta.authenticate(request)
        except Exception as e:
            print(e)
            result = {'return': 'fail', 'message': 'auth fail.'}
            return Response(data=result, status=403)

        if user_name != request.user:
            result = {'return': 'fail', 'message': 'others token.'}
            return Response(data=result, status=403)

        validated_data = dict()
        validated_data['name'] = request.data['name']
        validated_data['jenkins_job'] = request.data['jenkins_job']
        validated_data['git_url'] = request.data['git_url']
        validated_data['script_url'] = request.data['script_url']
        validated_data['manage_user'] = request.user

        try:
            App.objects.create(**validated_data)
            mylog.debug("create app is {}.".format(validated_data))
            response_data = {'result': 'success', 'message': u'新App服务应用插入数据库成功！'}
            return Response(data=response_data, status=status.HTTP_201_CREATED)
        except Exception as e:
            print(e)
            response_data = {'retult': 'error', 'message': u'App服务应用不能正确插入数据库！'}
            return Response(data=response_data, status=status.HTTP_400_BAD_REQUEST)

    def update(self, request, *args, **kwargs):
        try:
            ta = TokenAuthentication()
            user_name, token = ta.authenticate(request)
        except Exception as e:
            print(e)
            result = {'return': 'fail', 'message': 'auth fail.'}
            return Response(data=result, status=403)

        if user_name != request.user:
            result = {'return': 'fail', 'message': 'others token.'}
            return Response(data=result, status=403)

        validated_data = dict()
        validated_data['name'] = request.data['name']
        validated_data['jenkins_job'] = request.data['jenkins_job']
        validated_data['git_url'] = request.data['git_url']
        validated_data['script_url'] = request.data['script_url']
        validated_data['manage_user'] = request.user

        pk_id = kwargs["pk"]
        try:
            app_item = App.objects.filter(pk=pk_id)
            app_item.update(**validated_data)
            response_data = {'result': 'success', 'name': pk_id, 'create_user': request.user.username, 'message': u'更新App服务应用成功！'}
            return Response(data=response_data, status=status.HTTP_201_CREATED)
        except Exception as e:
            print(e)
            response_data = {'result': 'failed', 'message': u'更新App服务应用失败！'}
            return Response(data=response_data, status=status.HTTP_400_BAD_REQUEST)


class UserViewSet(viewsets.ReadOnlyModelViewSet):
    """This viewset automatically provides `list` and `detail` actions."""
    queryset = User.objects.all()
    serializer_class = UserSerializer
    permission_classes = (permissions.AllowAny, )

